package com.dabai.qrtools.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.HapticFeedbackConstants;
import android.view.SoundEffectConstants;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import java.util.Random;

/**
 * 手机通知栏消息工具类
 * 需要权限!!!!!!! 震动和通知栏
 * 触感反馈不需要权限
 *
 */
public class NotificationUtils {


    /**
     * 执行触觉反馈
     * 不需要震动权限
     */
    public static void performHapticFeedback(View view){
        view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
    }

    /**
     * 执行触觉反馈
     * 不需要震动权限
     */
    public static void performHapticFeedback(View view,boolean sound){
        view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
        if (sound){
            view.playSoundEffect(SoundEffectConstants.CLICK);
        }
    }

    /**
     * 执行触觉反馈
     * 不需要震动权限
     */
    public static void performHapticFeedback(View view,int hapticFeedbackConstants,boolean sound){
        view.performHapticFeedback(hapticFeedbackConstants,HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
        if (sound){
            view.playSoundEffect(SoundEffectConstants.CLICK);
        }
    }


    /**
     * 震动 默认100毫秒
     *
     * @param context 上下文
     */
    public static void vibrator(Context context) {
        int time = 100;
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        // Vibrate for time milliseconds
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate(VibrationEffect.createOneShot(time, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            //deprecated in API 26
            v.vibrate(time);
        }
    }

    /**
     * 振动器
     * 震动 自定义时间 (毫秒)
     *
     * @param context      上下文
     * @param milliseconds 毫秒
     */
    public static void vibrator(Context context,int milliseconds) {
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        // Vibrate for time milliseconds
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate(VibrationEffect.createOneShot(milliseconds, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            //deprecated in API 26
            v.vibrate(milliseconds);
        }
    }


    /**
     * 通知文本
     * 适配了 Android O± 的简单文字通知
     *
     * @param context   上下文
     * @param smallIcon 小图标
     * @param title     标题
     * @param content   内容
     */
    public static long notificationText(Context context,@NonNull String channelId,@NonNull String channelName,@NonNull int smallIcon,@NonNull String title, @NonNull String content) {

        //通知 id
        int id = new Random().nextInt(1000);

        Intent appIntent = new Intent(context,context.getClass());

        appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步，设置启动模式
        PendingIntent contentIntent =PendingIntent.getActivity(context, 0,appIntent,0);

        Notification notification = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(smallIcon)
                .setContentTitle(title)
                .setContentText(content)
                .setContentIntent(contentIntent)
                .build();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;


        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            //通知 强度
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
            notificationManager.createNotificationChannel(channel);

        }
        notificationManager.notify(id, notification);
        return id;
    }


}
